<?php

namespace app\wechat\service;
use Curl\CurlHttpRequest;
use EasyWeChat\Factory;

class Official
{
    public $config;
    public function __construct($confg=[])
    {
        if(!$confg)
        {
            $confg = get_option("wechat_official")['official'];
        }

        $this->config = array_merge($confg,config("official."));
    }

    /**
     * @Notes:获取code
     * @User: chenping
     * @email: 375129701@qq.com
     */
    public function get_code($url='https://new.xgzrs.com/wechat/home.index/prcode')
    {
        $option = $this->config;
        $callbackUrl =$url?urldecode($url):urldecode(url("wechat/oauth/wxcallback",'','',true));
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$option['app_id']}&redirect_uri={$callbackUrl}&response_type=code&state=state&scope=snsapi_base";
        header("Location:{$url}");
    }


    /**
     * @Notes:获取token
     * @User: chenping
     * @email: 375129701@qq.com
     */
    public function get_token()
    {
        $option = $this->config;
        $access_token  = (new CurlHttpRequest())->curlHttpGet("https://api.weixin.qq.com/cgi-bin/token",[
            "appid" => $option['app_id'],
            "secret" => $option['secret'],
            "grant_type" => "client_credential",
        ]);
        return json_decode($access_token,true);
    }

    /**
     * @Notes:获取用户信息
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $token
     * @return mixed|string
     */
    public function get_userinfo($param)
    {
        $user   = (new CurlHttpRequest())->curlHttpGet("https://api.weixin.qq.com/cgi-bin/user/info",[
            "access_token" => $param['access_token'],
            "openid"       => $param['openid'],
            "lang"         => "zh_CN"
        ]);
        return json_decode($user,true);
    }


    /**
     * @Notes: 模板消息推送
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $openid 用户在公众号得openid
     * @param $template_id 推送模板消息id
     * @param $data 推送得数据，根据模板消息规范定义
     * @param string $url 模板消息所需要跳转得链接
     * @param string $appid 小程序appid
     * @param string $pagepath 小程序所需要跳转得路由
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     */
    public function push_template_msg($openid,$template_id,$data,$url='',$appid='',$pagepath='')
    {
        $app = Factory::officialAccount($this->config);
        return $app->template_message->send([
            "touser"      => $openid,
            "template_id" => $template_id,
            "data"        => $data,
            "url"         => $url,
            "miniprogram" => [
                "appid"   => $appid,
                "pagepath"=> $pagepath,
            ],
        ]);
    }

    /**
     * @Notes:获取微信 授权 页（需第三方平台支持）
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $callBackUrl
     * @return string
     */
    public function getAuthorizationUrl($callBackUrl)
    {
        $openPlatform = Factory::openPlatform($this->config);
        return $openPlatform->getPreAuthorizationUrl($callBackUrl);
    }

    /**
     * @Notes:获取已经授权的用户信息
     * @User: chenping
     * @email: 375129701@qq.com
     * @return $this|\Overtrue\Socialite\User
     */
    public function getAuthUser()
    {
        $app    = Factory::officialAccount($this->config);
        return  $app->oauth->user();
    }

}